package io.renren.system.sys.service.impl;

import io.renren.common.constant.Constant;
import io.renren.common.exception.ErrorCode;
import io.renren.common.exception.RenException;
import io.renren.common.service.impl.BaseServiceImpl;
import io.renren.common.utils.ConvertUtils;
import io.renren.common.utils.HttpContextUtils;
import io.renren.common.utils.TreeUtils;
import io.renren.system.security.user.UserDetail;
import io.renren.system.sys.dao.SysMenuDao;
import io.renren.system.sys.dto.SysMenuDTO;
import io.renren.system.sys.entity.SysMenuEntity;
import io.renren.system.sys.enums.SuperAdminEnum;
import io.renren.system.sys.service.SysLanguageService;
import io.renren.system.sys.service.SysMenuService;
import io.renren.system.sys.service.SysRoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 系统设置-菜单管理
 */
@Service
public class SysMenuServiceImpl extends BaseServiceImpl<SysMenuDao, SysMenuEntity> implements SysMenuService {
    @Autowired
    private SysRoleMenuService sysRoleMenuService;
    @Autowired
    private SysLanguageService sysLanguageService;

    /**
     * 根据用户信息获取用户菜单列表
     *
     * @param user
     * @param type
     * @return
     */
    @Override
    public List<SysMenuDTO> getUserMenuList(UserDetail user, Integer type) {
        List<SysMenuEntity> menuList;
        //系统管理员，拥有最高权限
        if (user.getSuperAdmin() == SuperAdminEnum.YES.value()) {
            menuList = baseDao.getMenuList(type, HttpContextUtils.getLanguage());
        } else {
            menuList = baseDao.getUserMenuList(user.getId(), type, HttpContextUtils.getLanguage());
        }
        List<SysMenuDTO> dtoList = ConvertUtils.sourceToTarget(menuList, SysMenuDTO.class);
        return TreeUtils.build(dtoList);
    }

    /**
     * 获取全部菜单列表
     *
     * @param type
     * @return
     */
    @Override
    public List<SysMenuDTO> getAllMenuList(Integer type) {
        List<SysMenuEntity> menuList = baseDao.getMenuList(type, HttpContextUtils.getLanguage());
        List<SysMenuDTO> dtoList = ConvertUtils.sourceToTarget(menuList, SysMenuDTO.class);
        return TreeUtils.build(dtoList, Constant.MENU_ROOT);
    }

    /**
     * 根据父菜单查询子菜单
     *
     * @param pid
     * @return
     */
    @Override
    public List<SysMenuDTO> getListPid(Long pid) {
        List<SysMenuEntity> menuList = baseDao.getListPid(pid);
        return ConvertUtils.sourceToTarget(menuList, SysMenuDTO.class);
    }

    /**
     * 根据主键获取菜单详细信息
     *
     * @param id
     * @return
     */
    @Override
    public SysMenuDTO get(Long id) {
        SysMenuEntity entity = baseDao.getById(id, HttpContextUtils.getLanguage());
        SysMenuDTO dto = ConvertUtils.sourceToTarget(entity, SysMenuDTO.class);
        return dto;
    }

    /**
     * 新增保存菜单信息
     *
     * @param dto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(SysMenuDTO dto) {
        SysMenuEntity entity = ConvertUtils.sourceToTarget(dto, SysMenuEntity.class);
        //保存菜单
        insert(entity);
        this.saveLanguage(entity.getId(), "name", entity.getName());
    }

    /**
     * 修改保存菜单信息
     *
     * @param dto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysMenuDTO dto) {
        SysMenuEntity entity = ConvertUtils.sourceToTarget(dto, SysMenuEntity.class);
        //上级菜单不能为自身
        if (entity.getId().equals(entity.getPid())) {
            throw new RenException(ErrorCode.SUPERIOR_MENU_ERROR);
        }
        //更新菜单
        updateById(entity);
        this.saveLanguage(entity.getId(), "name", entity.getName());
    }

    /**
     * 根据主键删除菜单信息
     *
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        //删除菜单
        deleteById(id);
        //删除菜单国际化
        sysLanguageService.deleteLanguage("sys_menu", id);
        //根据菜单编号删除角色菜单关系
        sysRoleMenuService.deleteByMenuId(id);
    }

    /**
     * 新增或修改保存国际化信息
     *
     * @param tableId
     * @param fieldName
     * @param fieldValue
     */
    private void saveLanguage(Long tableId, String fieldName, String fieldValue) {
        sysLanguageService.saveOrUpdate("sys_menu", tableId, fieldName, fieldValue, HttpContextUtils.getLanguage());
    }

}